package com.sbs.gaoxinqutoilet.utils;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;

/**
 * Created by zhagntianjia on 2016/9/23 0023.
 * 图片加载辅助类
 */

public class ImageLoaderUtils {


    /**
     * 加载网络图片
     *
     * @param context
     * @param url
     * @param view
     */
    public static void setImageUrl(Context context, String url, ImageView view) {
//        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.SOURCE).dontAnimate().
//
//                into(new GlideDrawableImageViewTarget(view) {
//                    @Override
//                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
//                        super.onResourceReady(resource, animation);
//                        LogUtils.e("图片",resource.toString());
//                        view.setImageDrawable(resource);
//
//                    }
//                });
        //  解决 部分 变绿问题

        Glide.with(context)
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(view);
    }

    /**
     * 加载网络图片
     *
     * @param context
     * @param url
     * @param view
     */
//    public static void setImageUrlAError(Context context, String url, ImageView view) {
//        CustomLruCache customLruCache = CustomLruCache.getInstance();
//
////        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).error(R.drawable.error) .thumbnail(0.2f).animate(R.anim.item_alpha_in).centerCrop().into(view);
//        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).thumbnail(0.2f).animate(R.anim.item_alpha_in).into(view);
//    }

//    public static void setImageUrlAError(Context context, String url, ImageView view, final ProgressBar pb) {
//
////        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).error(R.drawable.error) .thumbnail(0.2f).animate(R.anim.item_alpha_in).centerCrop().into(view);
//        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                //.thumbnail(0.2f).animate(R.anim.item_alpha_in)
//                .dontAnimate()
//                .into(new GlideDrawableImageViewTarget(view) {
//                    @Override
//                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
//                        super.onResourceReady(resource, animation);
//                        LogUtils.e("图片",resource.toString());
//                        view.setImageDrawable(resource);
//                        pb.setVisibility(View.GONE);
//                    }
//                });
//
////           Glide.with(context).load(url).thumbnail(1.0F).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL).priority(Priority.NORMAL)
////                   .placeholder(R.drawable.faren_id_a).into(view);
//
//
//    }

//    public static void setImageBytesAError(Context context, Bitmap bitmap, ImageView view, final ProgressBar pb) {
//
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
//        byte[] bytes=baos.toByteArray();
////        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).error(R.drawable.error) .thumbnail(0.2f).animate(R.anim.item_alpha_in).centerCrop().into(view);
//        Glide.with(context).load(bytes).diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                //.thumbnail(0.2f).animate(R.anim.item_alpha_in)
//                .dontAnimate()
//                .into(new GlideDrawableImageViewTarget(view) {
//                    @Override
//                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
//                        super.onResourceReady(resource, animation);
//                        LogUtils.e("图片",resource.toString());
//                        view.setImageDrawable(resource);
//                        pb.setVisibility(View.GONE);
//                    }
//                });
//
////           Glide.with(context).load(url).thumbnail(1.0F).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL).priority(Priority.NORMAL)
////                   .placeholder(R.drawable.faren_id_a).into(view);
//
//
//    }
    /**
     * 加载图片文件
     *
     * @param activity
     * @param file
     * @param view
     */
//    public static void setImageFile(Activity activity, File file, ImageView view) {
//        Glide.with(activity).load(file).diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().into(view);
//    }

    /**
     * 根据文件路径加载图片
     *
     * @param view
     */
//    public static void setImageFilePath(Activity activity, String filePath, ImageView view) {
//        Glide.with(activity).load(filePath).diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().into(view);
//    }

//    public static Bitmap getUrlBitmap(Activity activity, String url) {
//        Bitmap bitmap = null;
//        try {
//            bitmap = Glide.with(activity)
//                    .load(url)
//                    .asBitmap()
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
//                    .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
//                    .get();
////            Log.e("bitmapsize",bitmap.)
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        return bitmap;
//    }

    public static void setImageResourceId(Context context, Integer resource, ImageView view) {
        Glide.with(context).load(resource).thumbnail(0.2f)
                //.animate(R.anim.item_alpha_in)
                .into(view);
    }

}
